import datetime

from sqlalchemy import False_, ForeignKey, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.core.models.enums import SubscriptionType

from ...core.models.enums.subscription import SubscriptionStatus
from .base import TimedBaseModel


class Subscription(TimedBaseModel):
    __tablename__ = "subscription"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    user = relationship("User", back_populates="subscription")
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
    status: Mapped[SubscriptionStatus] = mapped_column(server_default="active")
    subscription_type: Mapped[SubscriptionType]
    activation_code = relationship("ActivationCode", back_populates="subscriptions")
    activation_code_id: Mapped[int] = mapped_column(
        ForeignKey("activation_code.id"), nullable=True
    )
    calendars = relationship("UserCalendar", back_populates="subscription")
    start_date: Mapped[datetime.datetime] = mapped_column(server_default=func.now())
    notify_sent: Mapped[bool] = mapped_column(server_default=False_())
    end_date: Mapped[datetime.datetime]

    def __repr__(self):
        return f"<Подписка №{self.id}, пользователь №{self.user_id}>"

    def __str__(self):
        return self.__repr__()
